<?php
/**
*+-------IMBA后台管理视频数据的存储操作-----------+
*+-------------------------------------------------+
*+------------此模型是专用于单局比赛的-----------------+
*+-------------------------------------------------+
*+---------------基于MongoDB存储的-----------------+
*+-------------------------------------------------+
*+-@author susy.liang <liangling@ilongyuan.com.cn>-+
*+-------------------------------------------------+
*/
namespace Admin\Service;
use \Common\Service\BaseService;

Class VideoService extends BaseService
{
	/**
	* Base:
	 *	id, title, detail, thumb, status, create_time, update_time, time
	 *	uid, type ( normal, match )
	 *	group_id:比赛所属的赛程的标识
	 *extend:
	*
	*	game_id, match_id(赛事标识)
	*  competitor=array(1,2);参赛选手
	*  round:场次,score:胜负比, score_point:得分点即人头比
	*  time_group:时间组
	*  sort : 排序,  winner:胜利一方
	*
	*race_id (赛事下的比赛的标识)
	 */

	// 状态
	const STATUS_PEDDING = 0;	// 审核 pedding
	const STATUS_PASS 	 = 1;	// 展示 pass
	const STATUS_DEL  	 = -1;  // 删除 delete

	// 是否推荐
	const RECOMMEND_YES = 1;
	const RECOMMEND_NO  = 0;

	// 类型
	const TYPE_NORMAL = 0;	// 普通Video obj normal   普通视频
	const TYPE_MATCH  = 1;    // 比赛Match obj match    获取比赛的数据   

	// 分P类型
	const PAGE_SINGLE = 'single';	// 单P  single
	const PAGE_MULTI  = 'multi';	// 多P	multi

	//ERROR 错误
	const ERROR_TITLE = '标题不能为空';
	//const ERROR_THUMB = '未上传缩略图';比赛封面非必传
	const ERROR_USER  = '用户信息错误';
	const ERROR_GAME  = '游戏信息错误';
	const ERROR_TYPE  = '类型错误';
	//const ERROR_JOINER = '参赛队伍不能为空';

	protected $_validate = array(
		array('title', 'require', self::ERROR_TITLE, self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		//array('thumb', 'require', self::ERROR_THUMB, self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		array('uid', 'require', self::ERROR_USER, self::MUST_VALIDATE , 'regex', self::MODEL_INSERT),
		array('game_id', 'require', self::ERROR_GAME, self::MUST_VALIDATE , 'regex', self::MODEL_INSERT),
		array('type', 'require', self::ERROR_TYPE, self::MUST_VALIDATE , 'regex', self::MODEL_INSERT),
		array('score', 'checkScore', '比分格式错误', self::MUST_VALIDATE, 'callback'),
	);

	protected $_auto = array(
        array('uid', 'intval', self::MODEL_INSERT, 'function'),
        array('type', 'intval', self::MODEL_INSERT, 'function'),
        array('title', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        array('detail', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
        array('status', 'intval', self::MODEL_BOTH, 'function'),
        array('game_id', 'intval', self::MODEL_INSERT, 'function'),
        array('match_id', 'intval', self::MODEL_INSERT, 'function'),
 		array('group_id', 'intval', self::MODEL_BOTH, 'function'),
        array('match_time', 'strtotime', self::MODEL_BOTH, 'function'),
        array('match_date', 'getMatchDate', self::MODEL_BOTH, 'callback'),
    );  
	
	
	/**
	*设置视频所属的比赛
	*@param int $vid 视频标识
	*@param int $cmptId 赛事下的比赛标识
	*@return bool
	*@author susy<susy.liang@ilongyuan.com.cn>
	*/
	public function updateCmpt($vid, $cmptId){

		$res = $this->where(array('id'=>$vid))->save(array('cmpt_id'=>$cmptId));

		return $res?true:false;
	}

	protected function getMatchDate()
    {
    	return I('match_time','', 'trim');
    }

    /**
	*比分格式的校验
	*@author susy.liang<liangling@ilongyuan.com.cn>
	*/
	protected function checkScore($score){
		if(strpos($score, '-'))
			return true;
		else
			return false;
	}

}